package com.redrose.videodemo.base;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;


import com.redrose.videodemo.utils.ActivityCollector;
import com.redrose.videodemo.utils.EventBusMessage;
import com.redrose.videodemo.utils.LogUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.xutils.x;


public abstract class BaseActivity extends AppCompatActivity {
    protected Context mContext;

    protected final String TAG = BaseActivity.this.getClass().getSimpleName();
    /**
     * 是否旋转图片
     * true 表示当前Activity 处于横屏，false：当前处于竖屏
     */
    protected boolean mIsRotate = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        x.view().inject(this);
        EventBus.getDefault().register(this);
        //竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //横屏
//        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        ActivityCollector.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LogUtils.i(TAG, "onDestroy()");
        EventBus.getDefault().unregister(this);
        ActivityCollector.removeActivity(this);
    }

    @Subscribe
    public void onEventBusMessageRecieved(EventBusMessage message) {
        switch (message.getId()) {
            case EventBusMessage.MSG_TYPE_EXIT_APP:
                finish();
                break;
            default:
                break;
        }
    }

    public boolean hasPermission(String permission) {
        return PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(mContext, permission);
    }


    public void requestPeermission(String[] permissions, int requestCode) {
        ActivityCompat.requestPermissions(this, permissions, requestCode);
    }

    protected void showSnackbar(String str) {
        Snackbar.make(findViewById(android.R.id.content), str, Snackbar.LENGTH_LONG).show();
    }

    public void setIsRotate(boolean isRotate) {
        this.mIsRotate = isRotate;
    }

    public boolean getIsRotate() {
        return mIsRotate;
    }
}
